package com.lanyou.cook.repository;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

import com.lanyou.cook.entity.domain.FieldSystem;

public interface FieldSystemRepository
		extends Repository<FieldSystem, String>, JpaSpecificationExecutor<FieldSystem> {

	@RestResource(exported = false)
	FieldSystem findByCode(@Param("code") String code);
	
	@RestResource(exported = false)
	FieldSystem findByName(@Param("name") String name);
	
	@RestResource(exported = false)
	FieldSystem findById(String id);
	
	@RestResource(exported = false)
	FieldSystem findByCodeOrName(@Param("fieldSystemCode") String fieldSystemCode,@Param("name") String name);
	
	@RestResource(exported = false)
	long countByField_Id(String fieldId);
	
	@RestResource(exported = false)
	long countByUsers_Id(String userId);
	
	@RestResource(exported = false)
	long countById(String id);
	
	@RestResource(exported = false)
	FieldSystem findByNameAndField_Name(String name, String fieldName);
	
	@RestResource(exported = false)
	long countByCode(String code);
	
	long count();
	
	boolean exists(String id);
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"fieldSystemManagement:maintain"})
	<S extends FieldSystem> S save(S entity);	// could expose REST API
	
	<S extends FieldSystem> List<S> save(Iterable<S> entities);
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"fieldSystemManagement:view","fieldSystemManagement:maintain"},logical=Logical.OR)
	FieldSystem findOne(String id);	// could expose REST API
	
	FieldSystem getOne(String id);
	
	// 以下三个接口只能选一个暴露为REST API
	
	@RestResource(exported = false)
	List<FieldSystem> findAll();	// could expose REST API
	
	@RestResource(exported = false)
	List<FieldSystem> findAll(Sort sort);	// could expose REST API
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"fieldSystemManagement:view","fieldSystemManagement:maintain"},logical=Logical.OR)
	Page<FieldSystem> findAll(Pageable pageable);	// could expose REST API
	
	List<FieldSystem> findAll(Iterable<String> ids);
	
	// 以下两个接口，要么都暴露为REST API，要么都不暴露
	@RestResource(exported = true)
	@RequiresPermissions(value={"fieldSystemManagement:maintain"})
	void delete(String id);	// could expose REST API
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"fieldSystemManagement:maintain"})
	void delete(FieldSystem entity);	// could expose REST API
	
	void delete(Iterable<? extends FieldSystem> entities);
	
	void deleteAll();
	
	void flush();
	
	<S extends FieldSystem> S saveAndFlush(S entity);
	
	void deleteInBatch(Iterable<FieldSystem> entities);
	
	void deleteAllInBatch();
	@RestResource(exported = false)
	List<FieldSystem> findByUsers_UserCode(String userCode);
	
}
